/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

export function isEmpty(str) { // 判断输入框是否为空或者全为空格
  if (typeof str === 'undefined') {
    return false
  } else if (/^\s+$/g.test(str) || str.length === 0) {
    return true
  } else {
    return false
  }
}
export function isNullOrEmpty(obj) { // 判断所有数据是否为空
  const result = (obj == null || obj === 'null' || obj === undefined || obj === 'undefined' || typeof obj === 'undefined' || obj === '' || JSON.stringify(obj) === '{}' || JSON.stringify(obj) === '[]')
  if (result && (obj !== 0 || obj !== '0')) {
    return result
  }
  return false
}

export function validateWeb(rule, value, callback) {
  const webRegex = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/

  if (value) {
    if (!webRegex.test(value)) {
      callback('网址格式不正确！')
    } else {
      callback()
    }
  } else {
    if (rule.required) {
      callback(rule.message)
    } else {
      callback()
    }
  }
}

export function validateEmail(rule, value, callback) {
  const emilRegex = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
  if (value) {
    if (!emilRegex.test(value)) {
      callback('请输入正确的邮箱格式！')
    } else {
      callback()
    }
  } else {
    if (rule.required) {
      callback(rule.message)
    } else {
      callback()
    }
  }
}

export function validatePhoneNumber(rule, value, callback) {
  const phoneNumberRegex = /^1\d{10}$/
  if (value) {
    if (!phoneNumberRegex.test(value)) {
      callback('手机号格式不正确！')
    } else {
      callback()
    }
  } else {
    if (rule.required) {
      callback(rule.message)
    } else {
      callback()
    }
  }
}

export function validateIdCard(rule, value, callback) {
  const idcard = value.toUpperCase()
  const length = idcard.length
  // 判断身份证是否是15位或18位
  if (length !== 15 && length !== 18) {
    // 身份证长度不对
    callback('身份证长度不对！')
  } else {
    callback()
  }
}

export function validateTaxId(rule, value, callback) {
  const taxIdRegex = /[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g
  if (value) {
    if (!taxIdRegex.test(value)) {
      callback('统一信用代码格式不正确！')
    } else {
      callback()
    }
  } else {
    if (rule.required) {
      callback(rule.message)
    } else {
      callback()
    }
  }
}

// 校验非法字符
export function validateFeiFaZiFu(rule, value, callback) {
  const taxIdRegex = /^[\u4e00-\u9fa5_a-zA-Z0-9]+$/
  if (value) {
    if (!taxIdRegex.test(value)) {
      callback('非法字符！')
    } else {
      callback()
    }
  } else {
    if (rule.required) {
      callback(rule.message)
    } else {
      callback()
    }
  }
}
// 匹配空格
export function validateSpace(rule, value, callback) {
  const taxIdRegex = /(^\s+)|(\s+$)|\s+/g
  if (value) {
    if (taxIdRegex.test(value)) {
      callback('非法字符！')
    } else {
      callback()
    }
  } else {
    if (rule.required) {
      callback(rule.message)
    } else {
      callback()
    }
  }
}

export function checkFormSubmit(_this, formData, callback) {
  const canSubmit = []
  Object.keys(_this[formData]).forEach(item => {
    const refItem = Array.isArray(_this.$refs[item]) ? _this.$refs[item][0] : _this.$refs[item]
    if (refItem) {
      canSubmit.push(refItem.checkValue())
    }
  })
  if (!canSubmit.includes(false)) {
    callback()
  } else {
    return false
  }
}
/**
 * 清空表单
 * @param {*} data
 */
export function resetForm(data) {
  Object.keys(data).forEach(item => {
    data[item] = undefined
  })
}

/**
 *
 * @param {Number} setDate  //需要设置的天数
 */
export function dateInfo(setDate) {
  const now = new Date()
  const startDate = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate() - setDate)).toISOString().slice(0, 10)
  const endDate = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate())).toISOString().slice(0, 10)
  const date = []
  date.push(startDate)
  date.push(endDate)
  return date
}
/**
 *
 * @param {String} type  判断浏览器类型
 */
export function browserType(type) {
  switch (type) {
    case 'ie':
      if (!!window.ActiveXObject || 'ActiveXObject' in window) return true
      break
    default:
      return false
  }
}
export function changeMainBgColor() {
  var elHeight = -1 + document.getElementsByClassName('scroll-container')[0].clientHeight - document.getElementsByClassName('navbar')[0].clientHeight
  if (document.getElementsByClassName('app-main')[0]) {
    document.getElementsByClassName('app-main')[0].style.height = elHeight - 0 + 'px'
  }
}

export function getLabel(dictOptions, dictValue) {
  if ((dictValue && dictValue !== '') || dictValue === 0) {
    let obj = {}
    obj = dictOptions.find(item => item.value === dictValue)
    if (obj) {
      return obj.key
    } else {
      return dictValue
    }
  } else {
    return ''
  }
}

// 0-1之间得小数
export function validateZeroToOne(rule, value, callback) {
  const subCountRegex = /^(0\.[1-9]|0\.0[1-9]{1,2}?|0\.00[1-9]{1}?|0\.[1-9][0-9]{1,2}]?)$/
  if (value || value === '') {
    if (!subCountRegex.test(value)) {
      callback('格式不正确！')
    } else {
      callback()
    }
  } else {
    if (rule.required) {
      callback(rule.message)
    } else {
      callback()
    }
  }
}

// 清除form提示
export function resetTips(_this, Tips) {
  Object.keys(_this[Tips]).forEach(item => {
    if (_this.$refs[item] && _this.$refs[item].showTips) {
      _this.$refs[item].showTips = false
    }
  })
}

// 后台传过来的数据转为为label， value格式
/**
 * myRadio 接收 label value格式的数组
 * @param sourceList 有值的源数组
 * @param targetList 空数组
 * @param label myRadio的label 默认为id
 * @param value myRadio的value 默认为name
 * @returns {*} targetList
 */
export function setRadioKeyValue(sourceList, targetList, label, value) {
  if (!label) label = 'id'
  if (!value) value = 'name'
  if (sourceList.length === 0) {
    return targetList
  } else {
    sourceList.forEach(item => {
      const data = {
        label: item[label],
        value: item[value]
      }
      targetList.push(data)
    })
    return targetList
  }
}

/**
 * @param url 传过来拼接的url
 */
export function getImageUrl(url) {
 return this.$store.state.user.imagePath+url
}
